package net.hzlj.platform.enterprise.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import net.hzlj.platform.enterprise.BootApp;
import net.hzlj.platform.enterprise.beans.UserBean;
import net.hzlj.platform.enterprise.beans.resp.RespBaseBean;
import net.hzlj.platform.enterprise.common.Constant;
import net.hzlj.platform.enterprise.common.EventBusParameter;
import net.hzlj.platform.enterprise.config.HttpConfig;
import net.hzlj.platform.enterprise.receiver.LocationNotification;
import net.hzlj.platform.enterprise.task.HttpRequestApi;
import net.hzlj.platform.enterprise.task.Task;
import net.hzlj.platform.enterprise.task.TaskListener;
import net.hzlj.platform.enterprise.utils.AppPreferencesUtils;
import net.hzlj.platform.enterprise.utils.PreferencesUtils;
import net.hzlj.platform.enterprise.utils.StringUtils;

import org.greenrobot.eventbus.EventBus;

/**
 * @Description: 初始化数据
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2016年8月24日16:55:20
 */
public class InitService extends Service implements TaskListener {

    private static final String TAG = InitService.class.getSimpleName();

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate...");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand...");
        refreshNotification();
        if (intent != null && !StringUtils.isEmpty(intent.getAction())) {
            String action = intent.getAction();
            Log.i(TAG, "action:" + action);
            if (Constant.Intent.ACTION_UPDATE_USER_INFO.equals(action)) {
                //更新当前用户信息
                uploadUserInfo();
            }
        }
        return START_NOT_STICKY;
    }

    private void refreshNotification() {
        LocationNotification.getInstance().createNotification(this);
        if (null != LocationNotification.getInstance().getNotification()) {
            startForeground(BIND_AUTO_CREATE, LocationNotification.getInstance()
                    .getNotification());
        }
    }

    private void uploadUserInfo() {
        //更新当前用户信息
        if (BootApp.getInstance().isLogin()){
            HttpRequestApi.getUserDetail(BootApp.getInstance().getUserId(), this);
        }
    }


    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        Log.i(TAG, "onBind...");
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy...");
    }

    @Override
    public void onTaskResult(Task task) {
        if (task.type == HttpConfig.Type.SELECT_RYMC_BY_ID) {
            if (task.isSuccess) {
                RespBaseBean<UserBean> respBaseBean = task.resData;
                if (respBaseBean.isSuccess()) {
                    UserBean userBean = respBaseBean.getValues().get(0);
                    AppPreferencesUtils.putObject(this, PreferencesUtils.KEY_USER_INFO, userBean);
                    AppPreferencesUtils.putString(this, PreferencesUtils.KEY_USER_ACCOUNT, userBean.getIdentityNO());
                    EventBus.getDefault().post(new EventBusParameter(EventBusParameter.ACTION_UPDATE_USER));
                }
            }
        }
    }

}
